const app = getApp();
const req = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Choose:[
      {id:1,value:50},
      { id:2, value:100},
      { id:3, value:200},
      { id:4, value:299},
      { id:5, value:500},
    ],
    money:'',
    otherMoney:'',
  },


  rechargeClik(e){ 
    let value = e.currentTarget.dataset.id
    let Body = value
    let money = ''
    switch(value){
      case 1:
        money=50
        break
      case 2:
        money=100
        break
      case 3:
        money=200
        break
      case 4:
        money=299
        break
     default:
        money=500
    }
    if (money){
      this.setData({
        money,
        Body,
        otherMoney:''
      })
    }
 
  },

  bindReplaceInput(e){ //其他今额
    let otherMoney =e.detail.value
    if (otherMoney){
      this.setData({
        otherMoney,
        Body: '0',
        money:''
      })
    }
  },

  payClik(){
    let t = this
    let { money, otherMoney} = t.data
 
    if (!money){ //输入其他金额
      let str = /^-?\d+(\.\d+)?$/
      if (!str.test(otherMoney)) {
        wx.showToast({
          title: '请输入正确的金额！',
          icon: 'none',
        })
        return
      }
  
    }
    let params = {}
    if (money){
      params.fee = money
    } else if (otherMoney){
      params.fee = otherMoney
    }
    // let params = { fee: '1' }

    req.wxReq(`/mini/wxPay`, "POST", params).then(res => {
      console.log('购买', res)
      wx.hideLoading();
      if (res.data.code == 200) {
        wx.requestPayment({
          timeStamp: res.data.result.timeStamp,
          nonceStr: res.data.result.nonceStr,
          package: res.data.result.packageValue,
          signType: 'MD5',
          paySign: res.data.result.paySign,
        
          success(res) {
            console.log('222222', res)
            if (res.errMsg = "requestPayment:ok") {
              // wx.switchTab({
              //   url: '../ueser/index',
              // })
             wx.navigateTo({
               url: '../user/index',
             })
            }
          },
          fail(res) {
            console.log(res)
          }
        })

      } 
      else {
        wx.showToast({
          title: res.data.message,
          icon:'none',
          duration:1000
        })
      }
    })
   
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  wx.setNavigationBarTitle({
    title:'余额充值'
  })
  },

 
})